<?php

/**
 * Item.
 *
 * PHP version 8.3
 *
 * @category Class
 *
 * @author   OpenAPI Generator team
 *
 * @see     https://openapi-generator.tech
 */

/**
 * Amazon Shipping API.
 *
 * The Amazon Shipping API is designed to support outbound shipping use cases both for orders originating on Amazon-owned marketplaces as well as external channels/marketplaces. With these APIs, you can request shipping rates, create shipments, cancel shipments, and track shipments.
 *
 * The version of the OpenAPI document: v2
 * Contact: swa-api-core@amazon.com
 * Generated by: https://openapi-generator.tech
 * Generator version: 7.9.0
 */

/**
 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */

namespace SpApi\Model\shipping\v2;

use SpApi\Model\ModelInterface;
use SpApi\ObjectSerializer;

/**
 * Item Class Doc Comment.
 *
 * @category Class
 *
 * @description An item in a package.
 *
 * @author   OpenAPI Generator team
 *
 * @see     https://openapi-generator.tech
 *
 * @implements \ArrayAccess<string, mixed>
 */
class Item implements ModelInterface, \ArrayAccess, \JsonSerializable
{
    public const DISCRIMINATOR = null;

    /**
     * The original name of the model.
     */
    protected static string $openAPIModelName = 'Item';

    /**
     * Array of property to type mappings. Used for (de)serialization.
     *
     * @var string[]
     */
    protected static array $openAPITypes = [
        'item_value' => '\SpApi\Model\shipping\v2\Currency',
        'description' => 'string',
        'item_identifier' => 'string',
        'quantity' => 'int',
        'weight' => '\SpApi\Model\shipping\v2\Weight',
        'liquid_volume' => '\SpApi\Model\shipping\v2\LiquidVolume',
        'is_hazmat' => 'bool',
        'dangerous_goods_details' => '\SpApi\Model\shipping\v2\DangerousGoodsDetails',
        'product_type' => 'string',
        'invoice_details' => '\SpApi\Model\shipping\v2\InvoiceDetails',
        'serial_numbers' => 'string[]',
        'direct_fulfillment_item_identifiers' => '\SpApi\Model\shipping\v2\DirectFulfillmentItemIdentifiers'];

    /**
     * Array of property to format mappings. Used for (de)serialization.
     *
     * @var string[]
     *
     * @phpstan-var array<string, string|null>
     *
     * @psalm-var array<string, string|null>
     */
    protected static array $openAPIFormats = [
        'item_value' => null,
        'description' => null,
        'item_identifier' => null,
        'quantity' => null,
        'weight' => null,
        'liquid_volume' => null,
        'is_hazmat' => null,
        'dangerous_goods_details' => null,
        'product_type' => null,
        'invoice_details' => null,
        'serial_numbers' => null,
        'direct_fulfillment_item_identifiers' => null];

    /**
     * Array of nullable properties. Used for (de)serialization.
     *
     * @var bool[]
     */
    protected static array $openAPINullables = [
        'item_value' => true,
        'description' => true,
        'item_identifier' => true,
        'quantity' => false,
        'weight' => true,
        'liquid_volume' => true,
        'is_hazmat' => true,
        'dangerous_goods_details' => true,
        'product_type' => true,
        'invoice_details' => true,
        'serial_numbers' => true,
        'direct_fulfillment_item_identifiers' => true,
    ];

    /**
     * If a nullable field gets set to null, insert it here.
     *
     * @var bool[]
     */
    protected array $openAPINullablesSetToNull = [];

    /**
     * Array of attributes where the key is the local name,
     * and the value is the original name.
     *
     * @var string[]
     */
    protected static array $attributeMap = [
        'item_value' => 'itemValue',
        'description' => 'description',
        'item_identifier' => 'itemIdentifier',
        'quantity' => 'quantity',
        'weight' => 'weight',
        'liquid_volume' => 'liquidVolume',
        'is_hazmat' => 'isHazmat',
        'dangerous_goods_details' => 'dangerousGoodsDetails',
        'product_type' => 'productType',
        'invoice_details' => 'invoiceDetails',
        'serial_numbers' => 'serialNumbers',
        'direct_fulfillment_item_identifiers' => 'directFulfillmentItemIdentifiers',
    ];

    /**
     * Array of attributes to setter functions (for deserialization of responses).
     *
     * @var string[]
     */
    protected static array $setters = [
        'item_value' => 'setItemValue',
        'description' => 'setDescription',
        'item_identifier' => 'setItemIdentifier',
        'quantity' => 'setQuantity',
        'weight' => 'setWeight',
        'liquid_volume' => 'setLiquidVolume',
        'is_hazmat' => 'setIsHazmat',
        'dangerous_goods_details' => 'setDangerousGoodsDetails',
        'product_type' => 'setProductType',
        'invoice_details' => 'setInvoiceDetails',
        'serial_numbers' => 'setSerialNumbers',
        'direct_fulfillment_item_identifiers' => 'setDirectFulfillmentItemIdentifiers',
    ];

    /**
     * Array of attributes to getter functions (for serialization of requests).
     *
     * @var string[]
     */
    protected static array $getters = [
        'item_value' => 'getItemValue',
        'description' => 'getDescription',
        'item_identifier' => 'getItemIdentifier',
        'quantity' => 'getQuantity',
        'weight' => 'getWeight',
        'liquid_volume' => 'getLiquidVolume',
        'is_hazmat' => 'getIsHazmat',
        'dangerous_goods_details' => 'getDangerousGoodsDetails',
        'product_type' => 'getProductType',
        'invoice_details' => 'getInvoiceDetails',
        'serial_numbers' => 'getSerialNumbers',
        'direct_fulfillment_item_identifiers' => 'getDirectFulfillmentItemIdentifiers',
    ];

    /**
     * Associative array for storing property values.
     */
    protected array $container = [];

    /**
     * Constructor.
     *
     * @param null|array $data Associated array of property values
     *                         initializing the model
     */
    public function __construct(?array $data = null)
    {
        $this->setIfExists('item_value', $data ?? [], null);
        $this->setIfExists('description', $data ?? [], null);
        $this->setIfExists('item_identifier', $data ?? [], null);
        $this->setIfExists('quantity', $data ?? [], null);
        $this->setIfExists('weight', $data ?? [], null);
        $this->setIfExists('liquid_volume', $data ?? [], null);
        $this->setIfExists('is_hazmat', $data ?? [], null);
        $this->setIfExists('dangerous_goods_details', $data ?? [], null);
        $this->setIfExists('product_type', $data ?? [], null);
        $this->setIfExists('invoice_details', $data ?? [], null);
        $this->setIfExists('serial_numbers', $data ?? [], null);
        $this->setIfExists('direct_fulfillment_item_identifiers', $data ?? [], null);
    }

    /**
     * Gets the string presentation of the object.
     *
     * @return string
     */
    public function __toString()
    {
        return json_encode(
            ObjectSerializer::sanitizeForSerialization($this),
            JSON_PRETTY_PRINT
        );
    }

    /**
     * Array of property to type mappings. Used for (de)serialization.
     */
    public static function openAPITypes(): array
    {
        return self::$openAPITypes;
    }

    /**
     * Array of property to format mappings. Used for (de)serialization.
     */
    public static function openAPIFormats(): array
    {
        return self::$openAPIFormats;
    }

    /**
     * Checks if a property is nullable.
     */
    public static function isNullable(string $property): bool
    {
        return self::openAPINullables()[$property] ?? false;
    }

    /**
     * Checks if a nullable property is set to null.
     */
    public function isNullableSetToNull(string $property): bool
    {
        return in_array($property, $this->getOpenAPINullablesSetToNull(), true);
    }

    /**
     * Array of attributes where the key is the local name,
     * and the value is the original name.
     */
    public static function attributeMap(): array
    {
        return self::$attributeMap;
    }

    /**
     * Array of attributes to setter functions (for deserialization of responses).
     */
    public static function setters(): array
    {
        return self::$setters;
    }

    /**
     * Array of attributes to getter functions (for serialization of requests).
     */
    public static function getters(): array
    {
        return self::$getters;
    }

    /**
     * The original name of the model.
     */
    public function getModelName(): string
    {
        return self::$openAPIModelName;
    }

    /**
     * Show all the invalid properties with reasons.
     *
     * @return array invalid properties with reasons
     */
    public function listInvalidProperties(): array
    {
        $invalidProperties = [];

        if (null === $this->container['quantity']) {
            $invalidProperties[] = "'quantity' can't be null";
        }

        return $invalidProperties;
    }

    /**
     * Validate all the properties in the model
     * return true if all passed.
     *
     * @return bool True if all properties are valid
     */
    public function valid(): bool
    {
        return 0 === count($this->listInvalidProperties());
    }

    /**
     * Gets item_value.
     */
    public function getItemValue(): ?Currency
    {
        return $this->container['item_value'];
    }

    /**
     * Sets item_value.
     *
     * @param null|Currency $item_value item_value
     */
    public function setItemValue(?Currency $item_value): self
    {
        if (is_null($item_value)) {
            array_push($this->openAPINullablesSetToNull, 'item_value');
        } else {
            $nullablesSetToNull = $this->getOpenAPINullablesSetToNull();
            $index = array_search('item_value', $nullablesSetToNull);
            if (false !== $index) {
                unset($nullablesSetToNull[$index]);
                $this->setOpenAPINullablesSetToNull($nullablesSetToNull);
            }
        }
        $this->container['item_value'] = $item_value;

        return $this;
    }

    /**
     * Gets description.
     */
    public function getDescription(): ?string
    {
        return $this->container['description'];
    }

    /**
     * Sets description.
     *
     * @param null|string $description the product description of the item
     */
    public function setDescription(?string $description): self
    {
        if (is_null($description)) {
            array_push($this->openAPINullablesSetToNull, 'description');
        } else {
            $nullablesSetToNull = $this->getOpenAPINullablesSetToNull();
            $index = array_search('description', $nullablesSetToNull);
            if (false !== $index) {
                unset($nullablesSetToNull[$index]);
                $this->setOpenAPINullablesSetToNull($nullablesSetToNull);
            }
        }
        $this->container['description'] = $description;

        return $this;
    }

    /**
     * Gets item_identifier.
     */
    public function getItemIdentifier(): ?string
    {
        return $this->container['item_identifier'];
    }

    /**
     * Sets item_identifier.
     *
     * @param null|string $item_identifier a unique identifier for an item provided by the client
     */
    public function setItemIdentifier(?string $item_identifier): self
    {
        if (is_null($item_identifier)) {
            array_push($this->openAPINullablesSetToNull, 'item_identifier');
        } else {
            $nullablesSetToNull = $this->getOpenAPINullablesSetToNull();
            $index = array_search('item_identifier', $nullablesSetToNull);
            if (false !== $index) {
                unset($nullablesSetToNull[$index]);
                $this->setOpenAPINullablesSetToNull($nullablesSetToNull);
            }
        }
        $this->container['item_identifier'] = $item_identifier;

        return $this;
    }

    /**
     * Gets quantity.
     */
    public function getQuantity(): int
    {
        return $this->container['quantity'];
    }

    /**
     * Sets quantity.
     *
     * @param int $quantity The number of units. This value is required.
     */
    public function setQuantity(int $quantity): self
    {
        if (is_null($quantity)) {
            throw new \InvalidArgumentException('non-nullable quantity cannot be null');
        }
        $this->container['quantity'] = $quantity;

        return $this;
    }

    /**
     * Gets weight.
     */
    public function getWeight(): ?Weight
    {
        return $this->container['weight'];
    }

    /**
     * Sets weight.
     *
     * @param null|Weight $weight weight
     */
    public function setWeight(?Weight $weight): self
    {
        if (is_null($weight)) {
            array_push($this->openAPINullablesSetToNull, 'weight');
        } else {
            $nullablesSetToNull = $this->getOpenAPINullablesSetToNull();
            $index = array_search('weight', $nullablesSetToNull);
            if (false !== $index) {
                unset($nullablesSetToNull[$index]);
                $this->setOpenAPINullablesSetToNull($nullablesSetToNull);
            }
        }
        $this->container['weight'] = $weight;

        return $this;
    }

    /**
     * Gets liquid_volume.
     */
    public function getLiquidVolume(): ?LiquidVolume
    {
        return $this->container['liquid_volume'];
    }

    /**
     * Sets liquid_volume.
     *
     * @param null|LiquidVolume $liquid_volume liquid_volume
     */
    public function setLiquidVolume(?LiquidVolume $liquid_volume): self
    {
        if (is_null($liquid_volume)) {
            array_push($this->openAPINullablesSetToNull, 'liquid_volume');
        } else {
            $nullablesSetToNull = $this->getOpenAPINullablesSetToNull();
            $index = array_search('liquid_volume', $nullablesSetToNull);
            if (false !== $index) {
                unset($nullablesSetToNull[$index]);
                $this->setOpenAPINullablesSetToNull($nullablesSetToNull);
            }
        }
        $this->container['liquid_volume'] = $liquid_volume;

        return $this;
    }

    /**
     * Gets is_hazmat.
     */
    public function getIsHazmat(): ?bool
    {
        return $this->container['is_hazmat'];
    }

    /**
     * Sets is_hazmat.
     *
     * @param null|bool $is_hazmat When true, the item qualifies as hazardous materials (hazmat). Defaults to false.
     */
    public function setIsHazmat(?bool $is_hazmat): self
    {
        if (is_null($is_hazmat)) {
            array_push($this->openAPINullablesSetToNull, 'is_hazmat');
        } else {
            $nullablesSetToNull = $this->getOpenAPINullablesSetToNull();
            $index = array_search('is_hazmat', $nullablesSetToNull);
            if (false !== $index) {
                unset($nullablesSetToNull[$index]);
                $this->setOpenAPINullablesSetToNull($nullablesSetToNull);
            }
        }
        $this->container['is_hazmat'] = $is_hazmat;

        return $this;
    }

    /**
     * Gets dangerous_goods_details.
     */
    public function getDangerousGoodsDetails(): ?DangerousGoodsDetails
    {
        return $this->container['dangerous_goods_details'];
    }

    /**
     * Sets dangerous_goods_details.
     *
     * @param null|DangerousGoodsDetails $dangerous_goods_details dangerous_goods_details
     */
    public function setDangerousGoodsDetails(?DangerousGoodsDetails $dangerous_goods_details): self
    {
        if (is_null($dangerous_goods_details)) {
            array_push($this->openAPINullablesSetToNull, 'dangerous_goods_details');
        } else {
            $nullablesSetToNull = $this->getOpenAPINullablesSetToNull();
            $index = array_search('dangerous_goods_details', $nullablesSetToNull);
            if (false !== $index) {
                unset($nullablesSetToNull[$index]);
                $this->setOpenAPINullablesSetToNull($nullablesSetToNull);
            }
        }
        $this->container['dangerous_goods_details'] = $dangerous_goods_details;

        return $this;
    }

    /**
     * Gets product_type.
     */
    public function getProductType(): ?string
    {
        return $this->container['product_type'];
    }

    /**
     * Sets product_type.
     *
     * @param null|string $product_type the product type of the item
     */
    public function setProductType(?string $product_type): self
    {
        if (is_null($product_type)) {
            array_push($this->openAPINullablesSetToNull, 'product_type');
        } else {
            $nullablesSetToNull = $this->getOpenAPINullablesSetToNull();
            $index = array_search('product_type', $nullablesSetToNull);
            if (false !== $index) {
                unset($nullablesSetToNull[$index]);
                $this->setOpenAPINullablesSetToNull($nullablesSetToNull);
            }
        }
        $this->container['product_type'] = $product_type;

        return $this;
    }

    /**
     * Gets invoice_details.
     */
    public function getInvoiceDetails(): ?InvoiceDetails
    {
        return $this->container['invoice_details'];
    }

    /**
     * Sets invoice_details.
     *
     * @param null|InvoiceDetails $invoice_details invoice_details
     */
    public function setInvoiceDetails(?InvoiceDetails $invoice_details): self
    {
        if (is_null($invoice_details)) {
            array_push($this->openAPINullablesSetToNull, 'invoice_details');
        } else {
            $nullablesSetToNull = $this->getOpenAPINullablesSetToNull();
            $index = array_search('invoice_details', $nullablesSetToNull);
            if (false !== $index) {
                unset($nullablesSetToNull[$index]);
                $this->setOpenAPINullablesSetToNull($nullablesSetToNull);
            }
        }
        $this->container['invoice_details'] = $invoice_details;

        return $this;
    }

    /**
     * Gets serial_numbers.
     */
    public function getSerialNumbers(): ?array
    {
        return $this->container['serial_numbers'];
    }

    /**
     * Sets serial_numbers.
     *
     * @param null|array $serial_numbers A list of unique serial numbers in an Amazon package that can be used to guarantee non-fraudulent items. The number of serial numbers in the list must be less than or equal to the quantity of items being shipped. Only applicable when channel source is Amazon.
     */
    public function setSerialNumbers(?array $serial_numbers): self
    {
        if (is_null($serial_numbers)) {
            array_push($this->openAPINullablesSetToNull, 'serial_numbers');
        } else {
            $nullablesSetToNull = $this->getOpenAPINullablesSetToNull();
            $index = array_search('serial_numbers', $nullablesSetToNull);
            if (false !== $index) {
                unset($nullablesSetToNull[$index]);
                $this->setOpenAPINullablesSetToNull($nullablesSetToNull);
            }
        }
        $this->container['serial_numbers'] = $serial_numbers;

        return $this;
    }

    /**
     * Gets direct_fulfillment_item_identifiers.
     */
    public function getDirectFulfillmentItemIdentifiers(): ?DirectFulfillmentItemIdentifiers
    {
        return $this->container['direct_fulfillment_item_identifiers'];
    }

    /**
     * Sets direct_fulfillment_item_identifiers.
     *
     * @param null|DirectFulfillmentItemIdentifiers $direct_fulfillment_item_identifiers direct_fulfillment_item_identifiers
     */
    public function setDirectFulfillmentItemIdentifiers(?DirectFulfillmentItemIdentifiers $direct_fulfillment_item_identifiers): self
    {
        if (is_null($direct_fulfillment_item_identifiers)) {
            array_push($this->openAPINullablesSetToNull, 'direct_fulfillment_item_identifiers');
        } else {
            $nullablesSetToNull = $this->getOpenAPINullablesSetToNull();
            $index = array_search('direct_fulfillment_item_identifiers', $nullablesSetToNull);
            if (false !== $index) {
                unset($nullablesSetToNull[$index]);
                $this->setOpenAPINullablesSetToNull($nullablesSetToNull);
            }
        }
        $this->container['direct_fulfillment_item_identifiers'] = $direct_fulfillment_item_identifiers;

        return $this;
    }

    /**
     * Returns true if offset exists. False otherwise.
     *
     * @param int $offset Offset
     */
    public function offsetExists($offset): bool
    {
        return isset($this->container[$offset]);
    }

    /**
     * Gets offset.
     *
     * @param int $offset Offset
     *
     * @return null|mixed
     */
    #[\ReturnTypeWillChange]
    public function offsetGet($offset): mixed
    {
        return $this->container[$offset] ?? null;
    }

    /**
     * Sets value based on offset.
     *
     * @param null|int $offset Offset
     * @param mixed    $value  Value to be set
     */
    public function offsetSet($offset, mixed $value): void
    {
        if (is_null($offset)) {
            $this->container[] = $value;
        } else {
            $this->container[$offset] = $value;
        }
    }

    /**
     * Unsets offset.
     *
     * @param int $offset Offset
     */
    public function offsetUnset($offset): void
    {
        unset($this->container[$offset]);
    }

    /**
     * Serializes the object to a value that can be serialized natively by json_encode().
     *
     * @see https://www.php.net/manual/en/jsonserializable.jsonserialize.php
     *
     * @return mixed returns data which can be serialized by json_encode(), which is a value
     *               of any type other than a resource
     */
    #[\ReturnTypeWillChange]
    public function jsonSerialize(): mixed
    {
        return ObjectSerializer::sanitizeForSerialization($this);
    }

    /**
     * Gets a header-safe presentation of the object.
     */
    public function toHeaderValue(): string
    {
        return json_encode(ObjectSerializer::sanitizeForSerialization($this));
    }

    /**
     * Array of nullable properties.
     */
    protected static function openAPINullables(): array
    {
        return self::$openAPINullables;
    }

    /**
     * Array of nullable field names deliberately set to null.
     *
     * @return bool[]
     */
    private function getOpenAPINullablesSetToNull(): array
    {
        return $this->openAPINullablesSetToNull;
    }

    /**
     * Setter - Array of nullable field names deliberately set to null.
     *
     * @param bool[] $openAPINullablesSetToNull
     */
    private function setOpenAPINullablesSetToNull(array $openAPINullablesSetToNull): void
    {
        $this->openAPINullablesSetToNull = $openAPINullablesSetToNull;
    }

    /**
     * Sets $this->container[$variableName] to the given data or to the given default Value; if $variableName
     * is nullable and its value is set to null in the $fields array, then mark it as "set to null" in the
     * $this->openAPINullablesSetToNull array.
     *
     * @param mixed $defaultValue
     */
    private function setIfExists(string $variableName, array $fields, $defaultValue): void
    {
        if (self::isNullable($variableName) && array_key_exists($variableName, $fields) && is_null($fields[$variableName])) {
            $this->openAPINullablesSetToNull[] = $variableName;
        }

        $this->container[$variableName] = $fields[$variableName] ?? $defaultValue;
    }
}
